import store from '@/store'
import Vue from 'vue'
/**
 * 页面跳转 navigateTo 保留当前页面，跳转到应用内的某个页面
 */
export const navigateTo = (orgUrl, option) => {
	if (!orgUrl) return
	let _option = option || {}
	_option.url = dealUrl(orgUrl.url,option)
	if (_option.url.indexOf("/pages/tideway/index") > -1) { //潮物说页面单独处理
		uni.switchTab(_option)
	} else {
		_option.fail = ()=>{uni.switchTab(_option)}//navigateTo 跳转失败采用 switchTab重试防止tabBar页面跳转失败
		uni.navigateTo(_option)
	}
}
/**
 * 整个项目的跳转，判断是否在iframe内
 */
export const locationTo = (url, replace = false) => {
	if (store.state.iframeIn) {
		top.location.href = url
	} else {
		if (replace) {
			window.location.replace(url)
		} else {
			window.location.href = url
		}
	}
}
/**
 * 页面跳转 redirectTo 关闭当前页面，跳转到应用内的某个页面
 */
export const redirectTo = (orgUrl, option) => {
	if (!orgUrl) return
	let _option = option || {}
	_option.url = dealUrl(orgUrl.url,option)
	uni.redirectTo(_option)
}
/**
 * 页面跳转 reLaunch 关闭所有页面，打开到应用内的某个页面。
 */
export const reLaunch = (orgUrl, option) => {
	if (!orgUrl) return
	let _option = option || {}
	_option.url = dealUrl(orgUrl.url,option)
	uni.reLaunch(_option)
}
/**
 * 页面跳转 switchTab 跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面
 */
export const switchTab = (orgUrl, option) => {
	if (!orgUrl) return
	let _option = option || {}
	_option.url = dealUrl(orgUrl.url,option)
	const homePageUrl = store.state.buryPoint.homePageUrl
	if (homePageUrl && _option.url.startsWith(homePageUrl)) {
		uni.navigateTo(_option)
		return
	}
	uni.switchTab(_option)
}
// import Vue from 'vue'
function dealUrl(url,option) {

	return url;
}

export default {
	navigateTo,
	switchTab,
	reLaunch,
	locationTo,
	redirectTo
}
